package com.harvest.scientific.ladder.page

import android.graphics.Color
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import com.harvest.scientific.ladder.R
import com.harvest.scientific.ladder.base.BaActivity
import com.harvest.scientific.ladder.databinding.ActInfo4Binding
import com.harvest.scientific.ladder.ext.asLifecycleOwner
import com.harvest.scientific.ladder.res.RememberBlankAntRes
import com.harvest.scientific.ladder.util.FlowBus
import com.harvest.scientific.ladder.util.FlowBusConstant
import com.harvest.scientific.ladder.util.StbarUtil
import com.harvest.scientific.ladder.util.ToastUtil
import com.harvest.scientific.ladder.wheel.wheelpicker.BankAccountPicker

class Info4Activity: BaActivity(), View.OnClickListener {

    private lateinit var binding: ActInfo4Binding

    private lateinit var bankAccountPicker: BankAccountPicker
    private var bankAccount: RememberBlankAntRes?= null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        StbarUtil.setStatusBar(this,false, Color.TRANSPARENT,true)
        binding = ActInfo4Binding.inflate(layoutInflater)
        setContentView(binding.root)
        initEvent()
        initTextChangedListener()
        initObserver()
        initData()
    }

    private fun initEvent() {
        binding.llBack.setOnClickListener(this)
        binding.tvNext.setOnClickListener(this)
        binding.iAccountWay.root.setOnClickListener(this)
        binding.tvProtocol.setOnClickListener(this)
    }

    private fun initTextChangedListener() {
        binding.etAccount.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable?) {
                checkNextStatus()
            }
        })
        binding.etSureAccount.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable?) {
                checkNextStatus()
            }
        })
    }

    private fun initObserver(){
        appModel.showLoading.observe(asLifecycleOwner) {
            if (it) showLoading() else dismissLoading()
        }
        appModel.collectionWaySuc.observe(asLifecycleOwner) {
            bankAccountPicker.setJson(it)
        }
        appModel.searchInfo4.observe(asLifecycleOwner) {
            if (it != null) {
                bankAccount = RememberBlankAntRes(
                    it.technicalBigMealLoad,
                    it.publicInstructionConstantProjectSuccessfulFaith
                )
                if (!it.publicInstructionConstantProjectSuccessfulFaith.isNullOrEmpty()) {
                    binding.iAccountWay.tvContent.text = it.publicInstructionConstantProjectSuccessfulFaith
                }
                if (!it.nationalStandNoisyClothSecondhand.isNullOrEmpty()) {
                    binding.etAccount.setText(it.nationalStandNoisyClothSecondhand)
                    binding.etSureAccount.setText(it.nationalStandNoisyClothSecondhand)
                }
            }
            checkNextStatus()
        }
        appModel.saveInfo4Suc.observe(asLifecycleOwner) {
            dismissLoading()
            if (it) {
                AmountSelectActivity.jump(this)
//                finish()
//                FlowBus.post (FlowBusConstant.CLOSE_INFO, Unit)
            }
        }
    }

    private fun initData() {
        bankAccountPicker =
            BankAccountPicker(
                this
            )
        appModel.rememberBlankAnt("collectionType")
        appModel.combNorthDrawer("5")
        appModel.greetMerryRabbit()
    }


    override fun onClick(v: View) {
       when(v){
           binding.llBack -> {
               onBackPressed()
           }
           binding.tvNext -> {
                if (bankAccount?.furnishedLetterConnection.isNullOrEmpty()) {
                    ToastUtil.showToast(getString(R.string.toast_info))
                    return
                }
                if (binding.etAccount.text.toString().trim().isNullOrEmpty() || binding.etSureAccount.text.toString().trim().isNullOrEmpty()) {
                   ToastUtil.showToast(getString(R.string.toast_info))
                   return
                }
                if (binding.etAccount.text.toString() != binding.etSureAccount.text.toString()) {
                    ToastUtil.showToast(getString(R.string.toast_info2))
                    return
                }
                showLoading()
                appModel.greetMerryRabbit()
                appModel.uniteDangerousMineral(
                    "2",
                    bankAccount?.furnishedLetterConnection ?: "" ,
                    binding.etAccount.text.toString()
                )
           }
           binding.iAccountWay.root -> {
               bankAccountPicker.setBodyWidth(140)
               bankAccountPicker.setIncludeSecrecy(false)
               bankAccountPicker.setOnOptionPickedListener { position, item ->
                   bankAccount = item as RememberBlankAntRes?
                   binding.iAccountWay.tvContent.text = bankAccountPicker.wheelView.formatItem(position)
               }
               bankAccountPicker.wheelLayout
                   .setOnOptionSelectedListener { position, item ->
                       bankAccountPicker.titleView.text = bankAccountPicker.wheelView.formatItem(position)
                   }
               bankAccountPicker.show()
           }
           binding.tvProtocol -> {
               WebViewActivity.jumProtocol(this@Info4Activity)
           }
       }
    }

    private fun checkNextStatus() {
        if (getNextStatus()) {
            binding.tvNext.setBackgroundResource(R.drawable.bg_comon_sure_26)
        } else {
            binding.tvNext.setBackgroundResource(R.drawable.bg_comon_disable_sure_26)
        }
    }

    private fun getNextStatus(): Boolean {
        if (binding.etAccount.text.toString().trim().isEmpty()
                || binding.etSureAccount.text.toString().trim().isEmpty()) {
            return false
        }
        if (binding.iAccountWay.tvContent.text.toString().trim().isEmpty()) {
            return false
        }
        return true
    }


}